package cn.edu.ctbu.datajpa3.controller;

import cn.edu.ctbu.datajpa3.entity.Student;
import cn.edu.ctbu.datajpa3.service.StudentService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import java.util.Optional;

@Controller
@RequestMapping("/students")
public class StudentController {

    private final StudentService studentService;

    public StudentController(StudentService studentService) {
        this.studentService = studentService;
    }

    // 显示学生信息，支持分页和条件查询
    @GetMapping("/thymeleaf")
    public String showStudents(@RequestParam(defaultValue = "0") int page,
                               @RequestParam(defaultValue = "10") int size,
                               @RequestParam(required = false) String name,
                               @RequestParam(required = false) String studentNumber,
                               Model model) {

        Pageable pageable = PageRequest.of(page, size);
        Page<Student> studentPage = studentService.findByNameOrStudentNumber(name, studentNumber, pageable);

        // 将分页数据传递到 Thymeleaf 模板
        model.addAttribute("students", studentPage.getContent()); // 当前页的数据
        model.addAttribute("currentPage", studentPage.getNumber()); // 当前页
        model.addAttribute("totalPages", studentPage.getTotalPages()); // 总页数
        model.addAttribute("totalItems", studentPage.getTotalElements()); // 总数据量

        // 保留查询条件，以便表单显示
        model.addAttribute("searchName", name);
        model.addAttribute("searchStudentNumber", studentNumber);

        return "student"; // 返回 Thymeleaf 模板名称
    }

    // 分页查询，支持多个属性查询（通过 AJAX）
    @GetMapping("/ajax")
    @ResponseBody
    public Page<Student> getStudentsPage(
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String studentNumber,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {

        Pageable pageable = PageRequest.of(page, size);
        return studentService.findByNameOrStudentNumber(name, studentNumber, pageable);
    }

    // 按姓名或学号查询学生信息
    @GetMapping("/search")
    @ResponseBody
    public Page<Student> searchStudents(
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String studentNumber,
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size) {

        Pageable pageable = PageRequest.of(page, size);
        return studentService.findByNameOrStudentNumber(name, studentNumber, pageable);
    }

    // 根据 ID 获取单个学生信息
    @GetMapping("/{id}")
    @ResponseBody
    public Student getStudentById(@PathVariable Integer id) {
        Optional<Student> student = studentService.findById(id);
        return student.orElse(null);
    }

    // 添加学生信息（通过 AJAX 提交）
    @PostMapping
    @ResponseBody
    public void addStudent(@RequestBody Student student) {
        studentService.save(student);
    }

    // 更新学生信息
    @PutMapping("/{id}")
    @ResponseBody
    public void updateStudent(@PathVariable Integer id, @RequestBody Student student) {
        student.setId(id);
        studentService.save(student);
    }

    // 删除学生信息
    @DeleteMapping("/{id}")
    @ResponseBody
    public void deleteStudent(@PathVariable Integer id) {
        studentService.delete(id);
    }
}
